<template>
  <div v-if="show" class="achievements-dialog">
    <div class="dialog-content">
      <h3>成就系统</h3>
      
      <div class="achievement-stats">
        <div class="stat">
          <div class="stat-value">{{ unlockedAchievements.length }}</div>
          <div class="stat-label">已解锁</div>
        </div>
        <div class="stat">
          <div class="stat-value">
            {{ ((unlockedAchievements.length / achievements.length) * 100).toFixed(1) }}%
          </div>
          <div class="stat-label">完成度</div>
        </div>
      </div>

      <div class="achievements-grid">
        <div 
          v-for="achievement in achievements" 
          :key="achievement.id"
          class="achievement-card"
          :class="{ completed: achievement.completed }"
        >
          <div class="achievement-icon">{{ achievement.icon }}</div>
          <div class="achievement-info">
            <div class="achievement-name">{{ achievement.name }}</div>
            <div class="achievement-description">
              {{ achievement.description }}
            </div>
            <div 
              v-if="!achievement.completed" 
              class="achievement-progress"
            >
              <div class="progress-bar">
                <div 
                  class="progress-fill"
                  :style="{ width: `${getProgress(achievement.id)}%` }"
                ></div>
              </div>
              <div class="progress-text">
                {{ achievement.progress }}/{{ achievement.maxProgress }}
              </div>
            </div>
            <div 
              v-else 
              class="completion-info"
            >
              <div class="completion-time">
                {{ formatTime(achievement.completedAt!) }}
              </div>
              <div 
                v-if="achievement.reward"
                class="reward-info"
              >
                奖励: 
                <span class="reward">
                  {{ formatReward(achievement.reward) }}
                </span>
              </div>
            </div>
          </div>
        </div>
      </div>

      <div class="dialog-buttons">
        <button @click="$emit('close')">关闭</button>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { computed } from 'vue'
import { useAchievementSystem } from '../composables/useAchievementSystem'
import type { Achievement } from '../types/social'

const props = defineProps<{
  show: boolean
}>()

const emit = defineEmits<{
  (e: 'close'): void
}>()

const {
  achievements,
  unlockedAchievements,
  getProgress
} = useAchievementSystem()

const formatTime = (date: Date) => {
  return new Date(date).toLocaleString()
}

const formatReward = (reward: Achievement['reward']) => {
  if (!reward) return ''

  switch (reward.type) {
    case 'rating':
      return `+${reward.value} 等级分`
    case 'title':
      return `称号: ${reward.value}`
    case 'badge':
      return `徽章: ${reward.value}`
    default:
      return ''
  }
}
</script>

<style scoped>
.achievements-dialog {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.5);
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 1000;
}

.dialog-content {
  background: white;
  padding: 20px;
  border-radius: 8px;
  min-width: 800px;
  max-height: 80vh;
  overflow-y: auto;
}

.achievement-stats {
  display: flex;
  justify-content: space-around;
  margin: 20px 0;
}

.stat {
  text-align: center;
}

.stat-value {
  font-size: 24px;
  font-weight: bold;
  color: #4CAF50;
}

.stat-label {
  color: #666;
}

.achievements-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
  gap: 20px;
  margin: 20px 0;
}

.achievement-card {
  display: flex;
  gap: 15px;
  padding: 15px;
  border-radius: 8px;
  background: #f5f5f5;
  transition: transform 0.2s;
}

.achievement-card:hover {
  transform: translateY(-2px);
}

.achievement-card.completed {
  background: #e8f5e9;
}

.achievement-icon {
  font-size: 32px;
  width: 48px;
  height: 48px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.achievement-info {
  flex: 1;
}

.achievement-name {
  font-weight: bold;
  margin-bottom: 5px;
}

.achievement-description {
  color: #666;
  font-size: 14px;
  margin-bottom: 10px;
}

.achievement-progress {
  margin-top: 10px;
}

.progress-bar {
  height: 4px;
  background: #ddd;
  border-radius: 2px;
  overflow: hidden;
}

.progress-fill {
  height: 100%;
  background: #4CAF50;
  transition: width 0.3s ease;
}

.progress-text {
  font-size: 12px;
  color: #666;
  text-align: right;
  margin-top: 2px;
}

.completion-info {
  margin-top: 10px;
  font-size: 12px;
  color: #666;
}

.reward-info {
  margin-top: 5px;
}

.reward {
  color: #f57c00;
  font-weight: bold;
}

.dialog-buttons {
  display: flex;
  justify-content: flex-end;
  margin-top: 20px;
}

button {
  padding: 8px 16px;
  border: none;
  border-radius: 4px;
  background-color: #4CAF50;
  color: white;
  cursor: pointer;
}

button:hover {
  background-color: #45a049;
}
</style> 